	var resourcesIdStr="";
	var setting = {check: {enable: true},data: {simpleData: {enable: true}}};
	var code;
	
	function setCheck(treeId) {
		var zTree = $.fn.zTree.getZTreeObj(treeId),
		py = $("#py").attr("checked")? "p":"",
		sy = $("#sy").attr("checked")? "s":"",
		pn = $("#pn").attr("checked")? "p":"",
		sn = $("#sn").attr("checked")? "s":"",
		type = { "Y":py + sy, "N":pn + sn};
		zTree.setting.check.chkboxType = type;
		showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
	}
	function showCode(str) {
		if (!code) code = $("#code");
		code.empty();
		code.append("<li>"+str+"</li>");
	}
	function zTreeOnCheck(e,treeId,treeNode){
		resourcesIdStr="";
        var treeObj=$.fn.zTree.getZTreeObj(treeId);
        nodes=treeObj.getCheckedNodes(true);
		 for ( var int = 0; int < nodes.length; int++){
			 var node = nodes[int];
			 if( node.checked==true){
				 var nodeId = node.id;
				 if(resourcesIdStr==""){
					 resourcesIdStr=nodeId;
				 }else{
					 resourcesIdStr = resourcesIdStr+","+nodeId;
				 }
			 }
		}    
   }
$(document).ready(function(){	
	var screenwidth, screenheight, mytop, getPosLeft, getPosTop;
	screenwidth = $(window).width();
    screenheight = $(window).height();
	//获取滚动条距顶部的偏移
	mytop = $(document).scrollTop();
	//计算弹出层的left
	getPosLeft = screenwidth / 2 - 260;
	//计算弹出层的top
	getPosTop = screenheight / 2 - 200;
	//css定位弹出层
	$("#addRoleDiv").css({ "left": getPosLeft, "top": getPosTop });
	$("#modifyRoleDiv").css({ "left": getPosLeft, "top": getPosTop });
	$("#queryRoleDiv").css({ "left": getPosLeft, "top": getPosTop });
	//当浏览器窗口大小改变时
	$(window).resize(function() {
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 200;
		getPosTop = screenheight / 2 - 200;
		$("#addRoleDiv").css({ "left": getPosLeft, "top": getPosTop + mytop });
		$("#modifyRoleDiv").css({ "left": getPosLeft, "top": getPosTop + mytop });
		$("#queryRoleDiv").css({ "left": getPosLeft, "top": getPosTop + mytop });
	});
	//当拉动滚动条时，弹出层跟着移动
	$(window).scroll(function() {
		screenwidth = $(window).width();
		screenheight = $(window).height();
		mytop = $(document).scrollTop();
		getPosLeft = screenwidth / 2 - 200;
		getPosTop = screenheight / 2 - 200;
		$("#addRoleDiv").css({ "left": getPosLeft, "top": getPosTop + mytop });
		$("#modifyRoleDiv").css({ "left": getPosLeft, "top": getPosTop + mytop });
		$("#queryRoleDiv").css({ "left": getPosLeft, "top": getPosTop + mytop });
	}); 
	//获取页面文档的高度
	var docheight = $(document).height();
	//追加一个层，使背景变灰
	$("body").append("<div id='greyBackGround'></div>");
	$("#greyBackGround").css({ "opacity": "0.1", "height": docheight });
	
	 $("#addRole").click(function() {
		$("#queryRoleName")[0].value="";
		$.fn.zTree.init($("#addResourcesTree"),setting,addRoleTree);
		$("#greyBackGround").css("display","block");
		$("#addRoleDiv").fadeIn("slow");
		 
	 });

	 $("#addRoleBtn").click(function(){
		 var tempResourcesIdStr="";
   	     var treeObj=$.fn.zTree.getZTreeObj("addResourcesTree");
   	     if(treeObj==null || treeObj==""){
   			 alert("请选择资源!");
   			 return;
   	     }
   	     var nodes=treeObj.getCheckedNodes(true);
   	     if(nodes==null || nodes==""){
   			 alert("请选择资源!");
   			 return;
   	     }
		 for ( var int = 0; int < nodes.length; int++){
			 var node = nodes[int];
			 if( node.checked==true){
				 var nodeId = node.id;
				 if(tempResourcesIdStr==""){
					tempResourcesIdStr=nodeId;
				 }else{
					tempResourcesIdStr = tempResourcesIdStr+","+nodeId;
				 }
			 }
		}
		if(tempResourcesIdStr==""){
		 alert("请选择资源!");
		 return;
		}
		 var roleName = $("#addResourcesRoleName")[0].value;
		 if(roleName==null || roleName==""){
			 alert("请输入角色名!");
			 return;
		 }
		 var receiveCustom = $("#addReceiveCustom")[0].value;
		 if(receiveCustom==null || receiveCustom==undefined || receiveCustom==""){
			 alert("请输入是否能接收客户");
			 return;
		 }
		 $.ajax({
             type: "POST",
             url:"../../authority/role/checkRoleName",
             data:"roleName="+roleName,
             async: false,
             error: function(request) {
                 alert("Connection error");
                 location.reload();
             },
             success: function(data) {
            	 if(data=="1"){
            		 alert("此角色已经被使用,请选择其他角色名!");
            		 return;
            	 }else{
            		 $.ajax({
                         type: "POST",
                         url:"../../authority/role/addRole",
                         data:"roleName="+roleName+"&resourcesIdStr="+tempResourcesIdStr,
                         async: false,
                         error: function(request) {
                             alert("Connection error");
                             location.reload();
                         },
                         success: function(data) {
							alert("添加成功!");
							location.reload();
                         }
                     }); 
            	 }
             }
         }); 
	 });
	 
	 
	 $("#addRoleBtnCancel").click(function() {
		 $("#greyBackGround").css("display","none");
		 $("#addRoleDiv").hide();
	 });

	 $("#modifyRoleBtn").click(function(){
		 var tempResourcesIdStr="";
   	     var treeObj=$.fn.zTree.getZTreeObj("modifyResourcesTree");
   	     if(treeObj==null || treeObj==""){
   			 alert("请选择资源!");
   			 return;
   	     }
   	     var nodes=treeObj.getCheckedNodes(true);
   	     if(nodes==null || nodes==""){
   			 alert("请选择资源!");
   			 return;
   	     }
		 var receiveCustom = $("#modifyReceiveCustom")[0].value;
		 if(receiveCustom==null || receiveCustom==undefined || receiveCustom==""){
			 alert("请输入是否能接收客户");
			 return;
		 }
		 for ( var int = 0; int < nodes.length; int++){
			 var node = nodes[int];
			 if( node.checked==true){
				 var nodeId = node.id;
				 if(tempResourcesIdStr==""){
					tempResourcesIdStr=nodeId;
				 }else{
					tempResourcesIdStr = tempResourcesIdStr+","+nodeId;
				 }
			 }
		}			 
		 var roleId = $("#modifyRoleId")[0].value;
		 if(roleId==null || roleId==""){
			 alert("数据异常,请刷新页面!");
			 return;
		 }
		 var roleName = $("#modifyRoleName")[0].value;
		 if(roleName==null || roleName==""){
			 alert("请输入角色名称!");
			 return;
		 }
		 $.ajax({
             type: "POST",
             data:"isUpdate=true&roleId="+roleId+"&roleName="+roleName,
             url:"../../authority/role/checkRoleName",
             async: false,
             error: function(request) {
                 alert("Connection error");
                 location.reload();
             },
             success: function(data){
            	 if(data=="1"){
            		 alert("此角色名已在使用，请更换角色名称!");
            		 return;
            	 }else{
            		 $.ajax({
                         type: "POST",
                         url:"../../authority/role/updateRole",
                         data:"roleName="+roleName+"&resourcesIdStr="+tempResourcesIdStr+"&roleId="+roleId+"&receiveCustom="+receiveCustom,
                         async: false,
                         error: function(request) {
                             alert("Connection error");
                             location.reload();
                         },
                         success: function(data) {
                        	 alert("修改成功!");
                        	$("#greyBackGround").css("display","none");
                     		$("#modifyRoleDiv").css("display","none");
            				location.reload();
                         }
                     }); 
            	 }
             }
         });
	 });
	 $("#modifyRoleBtnCancel").click(function() {
		 $("#greyBackGround").css("display","none");
		 $("#modifyRoleDiv").hide();
	 });

	 $("#queryRoleBtn").click(function(){
		 $("#greyBackGround").css("display","none");
		 $("#queryRoleDiv").css("display","none");
		 
	 });
	 $("#queryRoleBtnCancel").click(function(){
		 $("#greyBackGround").css("display","none");
		 $("#queryRoleDiv").css("display","none");
		 
	 });
	 
	 $("#queryRole").click(function(){
		 $("#queryForm").submit(); 
	 });	
});
	
function queryRole(roleId,roleName,receiveCustom){
	$("#queryRoleName")[0].value=roleName;
	$("#queryReceiveCustom").val(receiveCustom);
/*	if(receiveCustom=="1"){
		
		$("#queryReceiveCustom").val(receiveCustom);
		
	}else{
		
	}*/
	 $.ajax({
            type: "POST",
            url:"../../authority/role/getRoleById",
            data:"roleId="+roleId,
            async: false,
            error: function(request) {
                alert("Connection error");
                location.reload();
            },
            success: function(data){
           	var queryRoleTree=eval(data);
            	$.fn.zTree.init($("#queryResourcesTree"),setting,queryRoleTree);
        		$("#greyBackGround").css("display","block");
        		$("#queryRoleDiv").fadeIn("slow");
            }
        });
};

function modifyRole(roleId,roleName,receiveCustom){
	$("#modifyRoleId")[0].value=roleId;
	$("#modifyRoleName")[0].value=roleName;
	$("#modifyReceiveCustom").val(receiveCustom);
	$.ajax({
	        type: "POST",
	    url:"../../authority/role/getRoleById",
	    data:"roleId="+roleId,
	    async: false,
	    error: function(request) {
	        alert("Connection error");
	        location.reload();
	    },
	    success: function(data){
	    	var modifyRoleTree = eval(data);
	    	$.fn.zTree.init($("#modifyResourcesTree"),setting,modifyRoleTree);
			$("#greyBackGround").css("display","block");
			$("#modifyRoleDiv").fadeIn("slow");
	    }
	});
};

function delRole(roleId){
 $.ajax({
           type: "POST",
           url:"../../authority/role/delRole",
           data:"roleId="+roleId,
           async: false,
           error: function(data) {
               alert("Connection error");
               location.reload();
           },
           success: function(data) {
          	 if(data=="0"){
          		 alert("有员工正在使用此角色,您不能删除!");
          		 return;
          	 }
      		 alert("删除成功!");
          	 location.reload();
           }
});
};

function getRoleList(pageNow){
	$("#pageNow")[0].value=pageNow;
	$("#queryForm").submit();
}